import Vue from 'vue'
import App from './App'
import router from './router'
import store from './store'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import axios from './config/axios_config'
import qs from 'qs'

// 在vue脚手架中使用elementUI组件
Vue.use(ElementUI);

Vue.prototype.$axios = axios;
Vue.prototype.$qs = qs;
Vue.prototype.$handleRouters = setRoutes;

Vue.config.productionTip = false;

setRoutes();

function setRoutes() {
  axios.post("/component/findComponentByRole").then(res => {
    if (res.code === 200) {
      router.addRoutes(getRouters(res.data.components))
    }
  });
}

function getRouters(data) {
  let result = [];
  for (let item of data) {
    if (item.children.length !== 0) {
      let arr = getRouters(item.children);
      result.push({
        path: item.path,
        name: item.name,
        component: () => import('./views/' + item.component),
        children: arr
      });
    } else {
      result.push({path: item.path, name: item.name, component: () => import('./views/' + item.component)});
    }
  }
  return result;
}


new Vue({
  el: '#app',
  router,
  store,
  components: {App},
  template: '<App/>'
})
